package com.bloom.util;

import java.beans.PropertyEditorSupport;

/**
 * A editor which allows the translation between {@link String} and {@link Enum}
 */
@SuppressWarnings("unchecked")
public class EnumEditor extends PropertyEditorSupport {
	private Class clazz;

	public EnumEditor(Class clazz) {
		this.clazz = clazz;
	}

	public EnumEditor() {
	}

	public String getAsText() {
		return (getValue() == null ? "" : ((Enum) getValue()).name());
	}

	public void setAsText(String text) throws IllegalArgumentException {
		setValue(Enum.valueOf(clazz, text));
	}
}
